package com.itheima.api.pms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.pms.PmsProductCategoryAttributeRelationMapper;
import com.itheima.vo.pojo.PmsProductCategoryAttributeRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
@DubboService
public class pmsProductCategoryAttributeRelationServiceImpl
        extends ServiceImpl<PmsProductCategoryAttributeRelationMapper, PmsProductCategoryAttributeRelation> implements PmsProductCategoryAttributeRelationService {

    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;

    /**
     * 根据id获取PmsProductCategoryAttributeRelation集合
     * @param productCategoryId
     * @return
     */
    @Override
    public List<PmsProductCategoryAttributeRelation> getProductAttributeId(Integer productCategoryId) {

        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProductCategoryAttributeRelation::getProductCategoryId,productCategoryId);

        return pmsProductCategoryAttributeRelationMapper.selectList(lqw);

    }

    /**
     * 调用pcarService的保存方法
     * @param pcId
     * @param productAttributeIdList
     */
    @Override
    public void save(long pcId, ArrayList<Integer> productAttributeIdList) {
        PmsProductCategoryAttributeRelation pcar = new PmsProductCategoryAttributeRelation();
        //设置商品分类id
        pcar.setProductCategoryId(pcId);
        //设置商品分类对应的商品属性id,一个商品分类可能对应多个商品属性,循环实现批量添加
        for (Integer paId : productAttributeIdList) {
            pcar.setProductAttributeId(paId);
            pmsProductCategoryAttributeRelationMapper.insert(pcar);
        }
    }

    /**
     * 调用pcarService的更新方法
     * @param id
     * @param paList
     */
    @Override
    public void updateById(Integer id, ArrayList<Integer> paList) {
        PmsProductCategoryAttributeRelation pcar = new PmsProductCategoryAttributeRelation();
        //设置商品分类id
        pcar.setProductCategoryId(id);
        for (Integer paId : paList) {
            pcar.setProductAttributeId(paId);
            pmsProductCategoryAttributeRelationMapper.updateById(pcar);
        }
    }
}
